<?php
/*
 * @Author: your name
 * @Date: 2021-09-21 16:46:35
 * @LastEditTime: 2022-02-28 16:30:41
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /nlog/app/Http/Controllers/AdminController/Login.php
 */

namespace App\Http\Controllers\admin_controller;

use Illuminate\Support\Facades\Request;
use App\Models\admin_model\CrmDivision as CrmDivisionDb;

class CrmDivision extends BaseController
{

  public function divisionAdd()
  {
    return    redis_mysql_add('crm_division');
  }

  public function divisionEdit()
  {
    return  redis_mysql_edit('crm_division');
  }


  public function divisionSelOne()
  {
    return  redis_mysql_selone('crm_division');
  }

  public function divisionList()
  {
    // $limitStr = '';
    // if (Request::has('limit') && Request::has('page')) {
    //     $limitStr = limit_joint(Request::input('page'), Request::input('limit'));
    // }

    $whereStr = '';
    if (Request::has('keyWord')) {
      $keyWord = array_filter(Request::input('keyWord'), 'filtrfunction');
      if (!empty($keyWord)) {
        $whereStr .= havaWhereLikeJoinEqual($keyWord);
      }
    }

    // $list  =  CrmDivisionDb::divisionList( $whereStr, $limitStr);
    // $total =  CrmDivisionDb::divisionCount( $whereStr);

    $list  =   CrmDivisionDb::divisionAll($whereStr);
    return $this->create(200, '查询成功', ['list' =>  $this->tree($list)]);
  }


  private function tree($list)
  {
    $keyList = [];
    foreach ($list as $val) {
      $keyList[$val['id']] = $val;
    }
    $newList = [];
    foreach ($keyList  as  &$val) {
      if ($val['pid'] ==  0) {
        $newList[] = &$val;
      } else if (isset($keyList[$val['pid']])) {
        $keyList[$val['pid']]['children'][] =   &$val;
      }
    }
    return $newList;
  }

  // private function keyArray($areaListSon,$strIf = false){
  //     $keyArray = [];
  //     $strId = '';
  //     for ($j = 0; $j < count($areaListSon); $j++) {
  //         if (!isset($keyArray[$areaListSon[$j]['pid']])) {
  //             $keyArray[$areaListSon[$j]['pid']] = [];
  //             if($strIf){
  //                 $strId .=  $areaListSon[$j]['pid'].',';
  //             } 
  //         }
  //         $keyArray[$areaListSon[$j]['pid']][] = $areaListSon[$j];
  //     }
  //     if(!$strIf){
  //         return  $keyArray;
  //     }else{
  //         return   ['keyArray' => $keyArray ,'strId'=>substr( $strId,0,-1)];
  //     }
  // }


  // public function areaSelList(){
  //    $areaListSelList =   AreaDb::areaListSelList();
  //    $list = [];
  //    $keyArray = [];
  //    for($i=0;$i<count( $areaListSelList );$i++){
  //         if($areaListSelList[$i]['pid'] == 0){
  //             $list[] = $areaListSelList[$i];
  //         }else{
  //             if(!isset($keyArray[$areaListSelList[$i]['pid']])){
  //                 $keyArray[$areaListSelList[$i]['pid']] = [];
  //             }
  //             $keyArray[$areaListSelList[$i]['pid']][] = $areaListSelList[$i];
  //         }
  //    }
  //    for($i=0;$i<count( $list );$i++){
  //        if(isset($keyArray[ $list[$i]['id'] ] )){
  //          $list[$i]['child'] = $keyArray[ $list[$i]['id'] ];
  //        }  
  //    }
  //   return $this->create(200,'查询成功',$list);
  // }



}
